<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_ecommerce'.DS.'tables');	

class EcommerceModelCars extends JModel
{
	var $_product = NULL;
	var $_where = NULL;
	var $_result = 0;
	var	$_sotrang = 0;
	var $_tht = 0;
	var $_page = NULL;
	var $_type = NULL;
	
	function __construct()
	{
		global $mainframe;
		parent::__construct();
		$type = intval(JRequest::getVar('type', 0));
		$this->_type = $type;
	}
	function getList()
	{
		if(!$this->_product)
		{		
			global $mainframe;
			$this->_params = &$mainframe->getParams();
									
			if(!(JRequest::getVar('page',0))){
				$this->_tht = 1;
			}else {
				$this->_tht = (int)JRequest::getVar('page');
			}
			
			if($this->_type == 1){
				$query = "SELECT product_id FROM #__auction WHERE closed = 0";
				$this->_db->setQuery($query);
				$proid = $this->_db->loadResultArray();
				$strid = implode(',', $proid);
				$where = " AND id IN (".$strid.")";
			} else {			
				$where = " AND type = ".$this->_type." AND approve = 1 AND bought = 0";
			}	
			
			//get total record
			$query = "SELECT count(id) FROM #__pr_product WHERE published = 1 " . $where ;
			$this->_db->setQuery( $query );
			//tong so record
			$total = $this->_db->loadResult();
			$this->_result = $total;//print_r($total);exit();
			
			$sodong = 15;// so record trong 1 trang
			$this->_sotrang = ceil($total/$sodong);
			$vitridau = ($this->_tht - 1 )* $sodong;
			
			$query = "SELECT * FROM #__pr_product where published = 1 " . $where . " ORDER BY ordering  LIMIT " . $vitridau . " , " . $sodong ;
			//print_r($query);exit();
			$this->_db->setQuery($query);	
			$this->_product = $this->_db->loadObjectList();//print_r($this->_product); exit();
			
			return $this->_product;
		}
		//return $this->_product;
	}
	
	
	function getSotrang(){
		return $this->_sotrang;
	}
	
	function getTotalResult(){
		return $this->_result;
	}

	function getTrangHienTai(){
		return $this->_tht;
	}	
}
?>